package com.ht.demo.admin.controller.system;


import com.ht.demo.admin.annotation.LoginUser;
import com.ht.demo.common.appexception.AppException;
import com.ht.demo.common.appexception.ResultData;
import com.ht.demo.common.pojo.bo.system.SysMenuBO;
import com.ht.demo.common.pojo.bo.system.SysUserBO;
import com.ht.demo.common.pojo.qto.system.SysMenuQTO;
import com.ht.demo.common.pojo.vo.RouterVo;
import com.ht.demo.common.pojo.vo.TreeSelect;
import com.ht.demo.service.manager.system.SelectMenuManager;
import com.ht.demo.service.manager.system.SysRoleMenuManager;
import com.ht.demo.service.system.UserTokenManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("${basePath}")
public class SelectMenuController {
    @Resource
    private SelectMenuManager selectMenuManager;
    @Resource
    private UserTokenManager userTokenManager;
    @Resource
    private SysRoleMenuManager sysRoleMenuManager;
    /**
     * 根据当前登录用户查询菜单列表
     * 菜单列表若要实现通过菜单名称查询
     * 则需要前端进行菜单树构建，后端返回菜单列表即可
     * 目前此处未实现通过菜单参数查询，默认查询所有菜单数据
     * @return
     */
    @GetMapping("/system/menu/list")
    public ResultData selectMenuList(SysMenuQTO qto) throws AppException {
        List<SysMenuBO> data = selectMenuManager.selectMenuList(qto);
        return new ResultData(data);
    }

    @GetMapping("/system/menu/{sysMenuId}")
    public ResultData selectMenuById(@PathVariable Long sysMenuId) throws AppException {
        SysMenuBO data = selectMenuManager.selectMenuById(sysMenuId);
        return new ResultData(data);
    }

    /**
     * 获取菜单下拉树列表
     *
     * @param qto
     * @return
     * @throws AppException
     */
    @GetMapping("/system/menu/treeSelect")
    public ResultData<List<TreeSelect>> buildTreeSelect(SysMenuQTO qto) throws AppException {
        SysUserBO loginUser = userTokenManager.getLoginUser();
        if (loginUser != null) {
            qto.setSysUserId(loginUser.getSysUserId());
        }
        List<SysMenuBO> menuList = selectMenuManager.selectMenuList(qto);
        List<TreeSelect> treeSelectList = selectMenuManager.buildTreeSelect(menuList);
        return new ResultData<>(treeSelectList);
    }

    /**
     * 查询当前登录用户拥有的菜单列表，
     * 返回树结构数据
     * @param user
     * @return
     * @throws AppException
     */
    @GetMapping("/system/menu/selectMenuByUserId")
    public ResultData<List<TreeSelect>> selectMenuListByUserId(@LoginUser SysUserBO user) throws AppException {
        List<SysMenuBO> menuList = selectMenuManager.selectMenuListByUserId(user.getSysUserId());
        List<TreeSelect> treeSelectList = selectMenuManager.buildTreeSelect(menuList);
        return new ResultData<>(treeSelectList);
    }

    /**
     * 查询当前登录用户菜单列表
     * 转换为前端需要的路由结构返回
     * @param user
     * @return
     * @throws AppException
     */
    @GetMapping("/getRouters")
    public ResultData<List<RouterVo>> getRouters(@LoginUser SysUserBO user) throws AppException {
        List<SysMenuBO> menuList = selectMenuManager.selectMenuListByUserId(user.getSysUserId());
        List<SysMenuBO> menuTree = selectMenuManager.buildMenuTree(menuList);
        List<RouterVo> data = selectMenuManager.getRouters(menuTree);
        return new ResultData<>(data);
    }

    /**
     * 角色修改页面，根据角色id获取该角色拥有的菜单ID，和菜单树列表
     * @param sysRoleId
     * @return
     * @throws AppException
     */
    @GetMapping("/system/menu/selectMenuTreeByRoleId")
    public ResultData selectMenuTreeByRoleId(Long sysRoleId) throws AppException {
        //该角色拥有的权限，即下拉菜单列表中选中的菜单
        List<Long> checkedKeys = sysRoleMenuManager.getMenuIdListByUserId(sysRoleId);
        SysMenuQTO qto = new SysMenuQTO();
        List<SysMenuBO> menus = selectMenuManager.selectMenuList(qto);
        ResultData data = new ResultData();
        data.getExtraMap().put("checkedKeys", checkedKeys);
        data.getExtraMap().put("menus", selectMenuManager.buildTreeSelect(menus));
        return data;
    }


}
